﻿using Common.Tool.FileTools;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Common.Device.Enums.DeviceEnum;

namespace Common.Device.DeviceDb
{
    public class DeviceDbHelper
    {
        /// <summary>
        /// 设备数据库
        /// </summary>
        public static SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = $"Data Source={Environment.CurrentDirectory}\\db\\Device.db",
            DbType = DbType.Sqlite,
            IsAutoCloseConnection = true
        }, db =>
        {
            //不存在创建数据库，存在不会创建
            db.DbMaintenance.CreateDatabase();
            //创建表根据实体类
            db.CodeFirst.InitTables(typeof(AllDevice), typeof(UseDevice));
            IniDeviceDb();
        });
        /// <summary>
        /// 初始化设备数据库
        /// </summary>
        public static void IniDeviceDb()
        {
            db.Deleteable<AllDevice>().ExecuteCommand();
            //初始化数据
            if (!db.Queryable<AllDevice>().Any())
            {
                //所有支持的仪器
                List<AllDevice> allDevices = new List<AllDevice>()
                {
                   new AllDevice { temp=DeviceTempEnum.安规仪, type=DeviceTypeEnum.绝缘耐压, model= DeviceModelEnum.AT686, mode= new List<DeviceModeEnum> { DeviceModeEnum.RS232} ,descripe="高压绝缘电阻测试仪  数控50V～2500VDC"},
                   new AllDevice { temp=DeviceTempEnum.安规仪, type=DeviceTypeEnum.绝缘耐压, model= DeviceModelEnum.EEC7473, mode=new List<DeviceModeEnum> { DeviceModeEnum.RS232, DeviceModeEnum.USB_NI} ,descripe="交流耐压测试仪  20kVac/10mA"},
                   new AllDevice { temp=DeviceTempEnum.安规仪, type=DeviceTypeEnum.绝缘耐压, model= DeviceModelEnum.TH2689, mode=new List<DeviceModeEnum> { DeviceModeEnum.RS232} ,descripe="电容器漏电流/绝缘电阻测试仪  最大测试电压：800V，充电电流0.5mA～500mA"},
                   new AllDevice { temp=DeviceTempEnum.安规仪, type=DeviceTypeEnum.绝缘耐压, model= DeviceModelEnum.TH9201B, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS232} ,descripe="程控交直流耐压绝缘测试仪  耐压测试:AC:5kv/0.001mA-20mA，DC:6kv/0.1uA-5mA；绝缘电阻测试:0.1MΩ-50GΩ"},
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.温度采集, model= DeviceModelEnum.RSWSN012D, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS232},descripe="温湿度采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_4211_14D2_30V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_44081_14M2_10V_20V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_42141_14N2_10V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_42241_14N2_5V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_42241_14N2_30V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_44121_14F2_10V_20V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_44241_14F2_30V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.采集模块, type=DeviceTypeEnum.电压采集, model= DeviceModelEnum.ZH_42241_14F2_10V, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS485},descripe="电压采集模块" },
                   new AllDevice { temp=DeviceTempEnum.测量仪, type=DeviceTypeEnum.电阻仪, model= DeviceModelEnum.AT512, mode= new List<DeviceModeEnum>{ DeviceModeEnum.RS232},descripe="精密电阻测试仪,直流电阻0.1μΩ～110MΩ,0.02%" },
                   new AllDevice { temp=DeviceTempEnum.测量仪, type=DeviceTypeEnum.数字电桥, model= DeviceModelEnum.UTR2830E, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232 } ,descripe="优利德 UTR2830E系列LCR数字电桥"},
                   new AllDevice { temp=DeviceTempEnum.测量仪, type=DeviceTypeEnum.万用表, model= DeviceModelEnum.DM3058E, mode= new List < DeviceModeEnum > { DeviceModeEnum.USB_NI },descripe="普源精电 DM3058/E系列数字万用表" },
                   new AllDevice { temp=DeviceTempEnum.电源, type=DeviceTypeEnum.交流电源, model= DeviceModelEnum.AC11005, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232 } ,descripe="单相交流电源"},
                   new AllDevice { temp=DeviceTempEnum.电源, type=DeviceTypeEnum.交流电源, model= DeviceModelEnum.AC13003, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232 },descripe="三相交流电源" },
                   new AllDevice { temp=DeviceTempEnum.电源, type=DeviceTypeEnum.直流电源, model= DeviceModelEnum.IT6722A, mode= new List < DeviceModeEnum > { DeviceModeEnum.USB_NI } ,descripe="艾德克斯 IT6722A可编程直流电源"},
                   new AllDevice { temp=DeviceTempEnum.电源, type=DeviceTypeEnum.直流电源, model= DeviceModelEnum.UDP3305S_E, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232,DeviceModeEnum.USB_NI },descripe="优利德 UDP3000S系列 三通道可编程线性直流电源" },
                   new AllDevice { temp=DeviceTempEnum.电源, type=DeviceTypeEnum.直流电源, model= DeviceModelEnum.DP811, mode= new List < DeviceModeEnum > { DeviceModeEnum.USB_NI},descripe="普源(RIGOL)DP811可编程线性直流稳压电源" },
                   new AllDevice { temp=DeviceTempEnum.电子负载, type=DeviceTypeEnum.电子负载, model= DeviceModelEnum.IT8511, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232 } ,descripe="艾德克斯 IT8500+ 系列单路输入可编程直流电子负载"},
                   new AllDevice { temp=DeviceTempEnum.电子负载, type=DeviceTypeEnum.电子负载, model= DeviceModelEnum.IT8813, mode= new List < DeviceModeEnum > { DeviceModeEnum.USB_NI } ,descripe="艾德克斯 IT8800系列 高速高精度可编程直流电子负载"},
                   new AllDevice { temp=DeviceTempEnum.示波器, type=DeviceTypeEnum.示波器, model= DeviceModelEnum.DHO1202, mode= new List < DeviceModeEnum > { DeviceModeEnum.USB_NI },descripe="普源精电 DHO1000系列双通道高分辨率示波器" },
                   new AllDevice { temp=DeviceTempEnum.测试板, type=DeviceTypeEnum.FCT测试板, model= DeviceModelEnum.FctModBus, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS485 },descripe="盟讯测试板卡（ModBus）继电器控制、电平采集" },
                   new AllDevice { temp=DeviceTempEnum.测试板, type=DeviceTypeEnum.FCT测试板3, model= DeviceModelEnum.FctModBus3, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS485 },descripe="盟讯测试板卡（ModBus）继电器控制、电平采集" },
                   new AllDevice { temp=DeviceTempEnum.通信, type=DeviceTypeEnum.通信测试, model= DeviceModelEnum.RS232, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232 },descripe="串口通信" },
                   new AllDevice { temp=DeviceTempEnum.通信, type=DeviceTypeEnum.通信测试, model= DeviceModelEnum.TcpClient, mode= new List < DeviceModeEnum > { DeviceModeEnum.TCP_Client },descripe="TCP/IP通信" },
                   new AllDevice { temp=DeviceTempEnum.割草机车身域, type=DeviceTypeEnum.车身域, model= DeviceModelEnum.N110C, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232 },descripe="车身域串口通信" },
                   new AllDevice { temp=DeviceTempEnum.割草机智控域, type=DeviceTypeEnum.智控域, model= DeviceModelEnum.N110Z, mode= new List < DeviceModeEnum > { DeviceModeEnum.RS232 },descripe="智控域串口通信" },
                };
                db.Insertable(allDevices).ExecuteCommand();
            }
        }

        /// <summary>
        /// 添加使用仪器
        /// </summary>
        /// <param name="device">使用仪器对象</param>
        /// <param name="isInsert">新增</param>
        public static bool UpUseDevice(UseDevice device)
        {
            //新增
            if (db.Queryable<UseDevice>().Count(it => it.name == device.name) == 0)
            {
                return db.Insertable(device).ExecuteCommand() == 1;
            }
            //更新
            else
            {
                return db.Updateable<UseDevice>().
                     SetColumns(it => new UseDevice
                     {
                         temp = device.temp,
                         name = device.name,
                         model = device.model,
                         descripe = device.descripe,
                         addr = device.addr,
                         type = device.type,
                         mode = device.mode,
                         port = device.port,
                         parameter = device.parameter,
                         parameter2 = device.parameter2,
                     }).Where(it => it.name == device.name).ExecuteCommand() == 1;
            }
        }
    }
}
